//
//  Emoji.m
//  EmojiDraw
//
//  Created by Kzzang on 12-11-9.
//  Copyright (c) 2012年 Korean Cycle Team. All rights reserved.
//

#import "Emoji.h"
#import <QuartzCore/QuartzCore.h>

@implementation Emoji
@synthesize emojiString = _emojiString;
@synthesize delegate = _delegate;


#pragma mark - Setter

- (void) setEmojiString:(NSString *)emojiString {
    if (_emojiString != emojiString) {
        _emojiString = emojiString;
        [self setNeedsDisplay];
    }
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void) drawText:(NSString *)text x:(float)x y:(float)y {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSelectFont(context, "AppleColorEmoji", 100, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
    CGContextSetTextMatrix(context, xform);
    CGContextSetTextPosition(context, x, y+20); // 20 is y-axis offset pixels
    CGContextShowText(context, [text UTF8String], strlen([text UTF8String]));
}
- (void) drawText2:(NSString *)text x:(float)x y:(float)y {
    UIFont *font = [UIFont systemFontOfSize:100];
    [text drawAtPoint:CGPointMake(x, y) withFont:font];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    NSString *text = self.emojiString;
    //[self drawText:text x:50 y:0];
    //[self drawText2:text x:0 y:0];
    [text drawInRect:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f) withFont:[UIFont fontWithName:@"AppleColorEmoji" size:25.0f]];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    if (touch && self.delegate && [self.delegate respondsToSelector:@selector(emojiDidClickedWithEmojiString:)]) {
        [self.delegate emojiDidClickedWithEmojiString:self.emojiString];
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

}
@end
